﻿using System.Drawing;
using static System.Convert;

namespace InterLock.Equipmet
{
    /// <summary>
    /// 道岔
    /// </summary>
    public class Turnout : Equipment
    {
        private Pen blackPen = new Pen(Color.Black, 2);
        private Pen whitePen = new Pen(Color.White, 2);
        private Pen bluePen = new Pen(Color.Blue, 2);

        public override void Draw(Graphics g)
        {
            if ((Character & ToInt32(0X0001)) != 0) //双动右下
            {
                DrawDoubleBottomRight(g);
                Region = new Rectangle(X1, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0002)) != 0) //双动右上
            {
                DrawDoubleTopRight(g);
                Region = new Rectangle(X1, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0004)) != 0) //双动左下
            {
                DrawDoubleBottomLeft(g);
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0008)) != 0) //双动左上
            {
                DrawDoubleTopLeft(g);
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0010)) != 0) //单动右下
            {
                DrawSimpleBottomRight(g);
                Region = new Rectangle(X1, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0020)) != 0) //单动右上
            {
                DrawSimpleTopRight(g);
                Region = new Rectangle(X1, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0040)) != 0) //单动左下
            {
                DrawSimpleBottomLeft(g);
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0080)) != 0) //单动左上 18号道岔
            {
                DrawNo18(g);
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }
            else if ((Character & ToInt32(0X0100)) != 0) //单动左上 16号道岔
            {
                DrawNo16(g);
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }

            //绘制名称
            g.DrawString(Name, new Font("宋体", 9, FontStyle.Bold), Brushes.White, X1 - 10, Y1 + 5);
        }

        /// <summary>
        /// 编号16道岔
        /// </summary>
        /// <param name="g"></param>
        private void DrawNo16(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 + 70, Y1);
                g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(whitePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
                }
                g.DrawLine(whitePen, X1, Y1, X1 + 70, Y1);
            }
        }

        /// <summary>
        /// 编号18道岔
        /// </summary>
        /// <param name="g"></param>
        private void DrawNo18(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                }
                g.DrawLine(bluePen, X1 - 20, Y1, X1 - 200, Y1);
                g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
                g.DrawLine(bluePen, X1 - 80, Y1 - 80, X1 - 200, Y1 - 80);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 - 200, Y1);
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
                    g.DrawLine(bluePen, X1 - 80, Y1 - 80, X1 - 200, Y1 - 80);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 200, Y1);
                    g.DrawLine(whitePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(whitePen, X1 - 10, Y1 - 10, X1 - 80, Y1 - 80);
                    g.DrawLine(whitePen, X1 - 80, Y1 - 80, X1 - 200, Y1 - 80);
                }
                Region = new Rectangle(X1 - 10, Y1 - 10, 20, 20);
            }
        }

        /// <summary>
        /// 单动左下
        /// </summary>
        /// <param name="g"></param>
        private void DrawSimpleBottomLeft(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 + 240, Y1);
                g.DrawLine(bluePen, X1 - 10, Y1 + 10, X1 - 80, Y1 + 80);
                g.DrawLine(bluePen, X1 - 80, Y1 + 80, X1 - 110, Y1 + 80);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(bluePen, X1 - 10, Y1 + 10, X1 - 80, Y1 + 80);
                    g.DrawLine(bluePen, X1 - 80, Y1 + 80, X1 - 110, Y1 + 80);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(whitePen, X1 - 10, Y1 + 10, X1 - 80, Y1 + 80);
                    g.DrawLine(whitePen, X1 - 80, Y1 + 80, X1 - 110, Y1 + 80);
                }
                g.DrawLine(whitePen, X1, Y1, X1 + 240, Y1);
            }
        }

        /// <summary>
        /// 单动右上
        /// </summary>
        /// <param name="g"></param>
        private void DrawSimpleTopRight(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 - 30, Y1);
                g.DrawLine(bluePen, X1 + 10, Y1 - 10, X1 + 80, Y1 - 80);
                g.DrawLine(bluePen, X1 + 80, Y1 - 80, X1 + 250, Y1 - 80);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(bluePen, X1 + 10, Y1 - 10, X1 + 80, Y1 - 80);
                    g.DrawLine(bluePen, X1 + 80, Y1 - 80, X1 + 250, Y1 - 80);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(whitePen, X1 + 10, Y1 - 10, X1 + 80, Y1 - 80);
                    g.DrawLine(whitePen, X1 + 80, Y1 - 80, X1 + 250, Y1 - 80);
                }
                g.DrawLine(whitePen, X1, Y1, X1 - 30, Y1);
            }
        }

        /// <summary>
        /// 单动右下
        /// </summary>
        /// <param name="g"></param>
        private void DrawSimpleBottomRight(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)                          //反位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                }
                else if (FWJ)                       //定位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 - 30, Y1);
                g.DrawLine(bluePen, X1 + 10, Y1 + 10, X1 + 80, Y1 + 80);
                g.DrawLine(bluePen, X1 + 80, Y1 + 80, X1 + 130, Y1 + 80);
            }
            if (SJ)
            {
                if (FWJ)                          //反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(whitePen, X1 + 10, Y1 + 10, X1 + 80, Y1 + 80);
                    g.DrawLine(whitePen, X1 + 80, Y1 + 80, X1 + 130, Y1 + 80);
                }
                else if (!FWJ)    //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(bluePen, X1 + 10, Y1 + 10, X1 + 80, Y1 + 80);
                    g.DrawLine(bluePen, X1 + 80, Y1 + 80, X1 + 130, Y1 + 80);
                }
                g.DrawLine(whitePen, X1, Y1, X1 - 30, Y1);
            }
        }

        /// <summary>
        /// 双动左上
        /// </summary>
        /// <param name="g"></param>
        private void DrawDoubleTopLeft(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 40, Y1 - 40);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(bluePen, X1 - 10, Y1 - 10, X1 - 40, Y1 - 40);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 - 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(whitePen, X1 - 10, Y1 - 10, X1 - 40, Y1 - 40);
                }
                g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
            }
        }

        /// <summary>
        /// 双动左下
        /// </summary>
        /// <param name="g"></param>
        private void DrawDoubleBottomLeft(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                g.DrawLine(bluePen, X1 - 10, Y1 + 10, X1 - 40, Y1 + 40);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(bluePen, X1 - 10, Y1 + 10, X1 - 40, Y1 + 40);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 - 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 - 20, Y1);
                    g.DrawLine(whitePen, X1 - 10, Y1 + 10, X1 - 40, Y1 + 40);
                }
                g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
            }
        }

        /// <summary>
        /// 双动右上
        /// </summary>
        /// <param name="g"></param>
        private void DrawDoubleTopRight(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                }
                g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                g.DrawLine(bluePen, X1 + 10, Y1 - 10, X1 + 40, Y1 - 40);
            }
            if (SJ)
            {
                if (!FWJ)  //定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(bluePen, X1 + 10, Y1 - 10, X1 + 40, Y1 - 40);
                    g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
                }
                else if (FWJ)//反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 + 10, Y1 - 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(whitePen, X1 + 10, Y1 - 10, X1 + 40, Y1 - 40);
                }
                g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
            }
        }

        /// <summary>
        /// 双动右下
        /// </summary>
        /// <param name="g"></param>
        private void DrawDoubleBottomRight(Graphics g)
        {
            if (!SJ)  //未锁闭
            {
                g.DrawLine(bluePen, X1 + 10, Y1 + 10, X1 + 40, Y1 + 40);
                g.DrawLine(bluePen, X1, Y1, X1 - 20, Y1);
                if (FWJ)                          //反位
                {
                    g.DrawLine(bluePen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                }
                else if (!FWJ)
                {                                   //定位
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(bluePen, X1, Y1, X1 + 20, Y1);
                }
            }
            if (SJ)
            {
                if (FWJ)                          //反位
                {
                    g.DrawLine(whitePen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(blackPen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(whitePen, X1 + 10, Y1 + 10, X1 + 40, Y1 + 40);
                }
                else if (!FWJ)//定位
                {
                    g.DrawLine(blackPen, X1, Y1, X1 + 10, Y1 + 10);
                    g.DrawLine(whitePen, X1, Y1, X1 + 20, Y1);
                    g.DrawLine(bluePen, X1 + 10, Y1 + 10, X1 + 40, Y1 + 40);
                }
                g.DrawLine(whitePen, X1, Y1, X1 - 20, Y1);
            }
        }
    }
}
